package org.me.marble;

import android.view.*;
import android.widget.*;
//import android.graphics.*;

import android.opengl.GLSurfaceView;
import android.view.View.OnTouchListener;
import android.widget.SeekBar.OnSeekBarChangeListener;

/**
*
* @author tungi
*/

public class GameView implements OnTouchListener,OnSeekBarChangeListener{
	// extends GLSurfaceView{
    private GameRenderer _gamerenderer;

    public GameView(GameActivity game){
        //super(game);
        _gamerenderer=new GameRenderer(game);
        game.setRenderer(_gamerenderer);//to set renderer to main
        //setRenderer(_gamerenderer);//to GLSurfaceView
    }

    public void setContentView(){        
    	if(OptionsLibrary.sound)
        	SoundManager.stopSound(sounds.Loading.ordinal());
    	
        GameActivity.game.setContentView(R.layout.game);
                
        /*
        double paddingTop=Math.random();
        double paddingLeft=Math.random();
        int pic=(new java.util.Random()).nextInt(5)+1;
        int resId=GameActivity.game.getResources().
        			getIdentifier("background_game"+Integer.toString(pic),
        			"drawable",GameActivity.game.getPackageName());
        ((ImageView)GameActivity.game.findViewById(R.game.backimage)).
        	setBackgroundDrawable(new BackgroundImage(GameActivity.game,
			R.drawable.background_game,Color.BLACK,paddingTop,paddingLeft));
        */
        
        SeekBar onscreenControl=(SeekBar)GameActivity.game.findViewById(R.game.onscreencontrol);
        onscreenControl.setOnSeekBarChangeListener(this);
        if(OptionsLibrary.onscreen){
        	onscreenControl.setEnabled(true);
        	onscreenControl.setVisibility(View.VISIBLE);        	
        }
        else{
        	onscreenControl.setEnabled(false);
        	onscreenControl.setVisibility(View.INVISIBLE);        	
        }
        
        GLSurfaceView gameview=(GLSurfaceView)GameActivity.game.findViewById(R.game.gameview);
        //gameview.setDebugFlags(GLSurfaceView.DEBUG_CHECK_GL_ERROR | GLSurfaceView.DEBUG_LOG_GL_CALLS);
        gameview.setOnTouchListener(this);
        
        /*
        if(!OptionsLibrary.fog)
	        gameview.setBackgroundDrawable(new BackgroundImage(GameActivity.game,
				R.drawable.background_game,Color.BLACK,paddingTop,paddingLeft));
        
        if(!OptionsLibrary.fog){
            gameview.setEGLConfigChooser(8,8,8,8,16,0);//8,8,8,8,24,8
            gameview.setRenderer(_gamerenderer);
            gameview.getHolder().setFormat(PixelFormat.TRANSLUCENT);
            gameview.setZOrderOnTop(true);
        }
        else
        */
        
        gameview.setRenderer(_gamerenderer);
    }

    //@Override
    public boolean onTouch(View view,MotionEvent event){
        if(event.getAction()==MotionEvent.ACTION_DOWN  && GameActivity.game.isLevelInitialized())
            _gamerenderer.setPause(!_gamerenderer.isPause());
        return true;
    }

	@Override
	public void onProgressChanged(SeekBar seekBar,int progress,boolean fromUser){
		if(_gamerenderer.isPause())
			return;
		
		int sign=progress<50?1:-1;
		//double angle=sign*Math.log(Math.abs(progress-50)+1)/Math.log(51)s;
		//double angle=sign*(Math.abs(progress-50)+1)/51;
		double angle=sign*Math.pow((Math.abs(progress-50)+1)/(double)51,3);
		angle*=(Math.PI/3);
		_gamerenderer.rotateDirection(angle);
	}

	@Override
	public void onStartTrackingTouch(SeekBar seekBar){
	}

	@Override
	public void onStopTrackingTouch(SeekBar seekBar){
    	seekBar.setProgress(50);
	}
}
